phpDocumentor Web Commons
[ class tree: Web Commons ] [ index: Web Commons ] [ all elements ]

Source for file utils.php

Documentation is available at utils.php

  1. <?php
  2. /**
  3.  * This file offers various utility functions and classes.
  4.  * 
  5.  * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org>
  6.  * @license http://www.opensource.org/licenses/mit-license.php
  7.  * @version 0.1dev
  8.  */
  9.  
  10. /**
  11.  * Returns the value held by an $array at a given $key, or $default
  12.  * if this $key does not exist.
  13.  * @param array $array 
  14.  * @param mixed $key 
  15.  * @param mixed $default 
  16.  * @return mixed 
  17.  * @since 0.1
  18.  */
  19. function array_get_default(&$array$key$default{
  20.     if (array_key_exists($key$array))
  21.         return $array[$key];
  22.     else
  23.         return $default;
  24. }
  25.  
  26. /**
  27.  * Loads an ini file using {@link parse_ini_file()}, merging its data
  28.  * with a parent ini file if "extends" is defined at the root of the ini file.
  29.  * "extends" value is the relative or absolute path to the extended file.
  30.  * "extends" will not appear in resulting array.
  31.  * 
  32.  * Returns False on failure (file missing, unreadable or unparseable)
  33.  * 
  34.  * @param string $filename 
  35.  * @return array 
  36.  * @since 0.1
  37.  */
  38. function load_extensible_ini_file($filename{
  39.     if (!is_file($filename)) {
  40.         return False;
  41.     }
  42.     
  43.     $data parse_ini_file($filenameTrue);
  44.     
  45.     if ($data === False)
  46.         return False;
  47.     
  48.     $extends array_get_default($data'extends'Null);
  49.     unset($data['extends']);
  50.     
  51.     if ($extends{
  52.         if ($extends[0=== '/'{
  53.             /* absolute path */
  54.             $path $extends;
  55.         }
  56.         else {
  57.             /* relative path */
  58.             $dir dirname($filename);
  59.             $path realpath($dir DIRECTORY_SEPARATOR $extends);
  60.         }
  61.         
  62.         $extendedData load_extensible_ini_file($path);
  63.         
  64.         if ($extendedData !== False{
  65.             return array_merge_two_dimensional($extendedData$data);
  66.         }
  67.         else {
  68.             return False;
  69.         }
  70.     }
  71.     else {
  72.         return $data;
  73.     }
  74. }
  75.  
  76. /**
  77.  * Merges two two-dimensional arrays.
  78.  * If keys overlap, array1 will be overriden by array2.
  79.  * Both arrays are untouched.
  80.  * @param array $array1 
  81.  * @param array &$array2 
  82.  * @return array 
  83.  * @since 0.1
  84.  */
  85. function array_merge_two_dimensional($array1&$array2{
  86.     foreach ($array2 as $key=>$subarray{
  87.         if (array_key_exists($key$array1)) {
  88.             $array1[$keyarray_merge($array1[$key]$subarray);
  89.         }
  90.         else {
  91.             $array1[$key$subarray;
  92.         }
  93.     }
  94.     
  95.     return $array1;
  96. }

Documentation generated on Fri, 16 Jul 2010 00:48:40 +0200 by phpDocumentor 1.4.3